package com.aba.hos.pat;

import com.aba.hos.admin.AdminLoginExampleWindow;
import com.aba.hos.dao.PatDao;
import com.aba.hos.dao.PatWordDao;
import com.aba.hos.dao.impl.PatDaoImpl;
import com.aba.hos.dao.impl.PatWordDaoImpl;
import com.aba.hos.doc.entity.PatWord;
import com.aba.hos.doc.entity.Patient;
import com.aba.hos.totality.LoginWindow;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class patientExampleWindow extends JFrame {
    private PatDao patDao = new PatDaoImpl();
    private PatWordDao wordDao = new PatWordDaoImpl();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                new patientExampleWindow().openMainPage("张三", "病人");
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public void openMainPage(String username, String role) throws SQLException {
        JFrame mainFrame = new JFrame("主页面 - " + username);
        mainFrame.setSize(800, 600);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new BorderLayout());

        // 顶部欢迎面板
        JPanel topPanel = createTopPanel(username, role);
        mainPanel.add(topPanel, BorderLayout.NORTH);

        // 中心表格面板
        JPanel centerPanel = createPatientTablePanel(username,role);
        mainPanel.add(centerPanel, BorderLayout.CENTER);

        // 底部退出面板
        JPanel bottomPanel = createBottomPanel(mainFrame,role,username);
        mainPanel.add(bottomPanel, BorderLayout.SOUTH);

        mainFrame.add(mainPanel);
        mainFrame.setVisible(true);
    }

    private JPanel createTopPanel(String username, String role) {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(240, 240, 240));
        panel.setPreferredSize(new Dimension(800, 80));

        JLabel welcomeLabel = new JLabel("欢迎您，" + role + " " + username + "!");
        welcomeLabel.setFont(new Font("宋体", Font.BOLD, 20));
        welcomeLabel.setForeground(new Color(50, 50, 50));
        panel.add(welcomeLabel);

        return panel;
    }

    private JPanel createPatientTablePanel(String username,String role) throws SQLException {
        List<PatWord> words = wordDao.queryPatWordByName(username);
        List<Patient> patientList =new ArrayList<>();
        Patient patients =  patDao.queryPatByName(username);
        patientList.add(patients);
        if(role.equals("管理员")){
            patientList =new ArrayList<>();
            patientList =  patDao.queryPatByNameAllList();
            words =new ArrayList<>();
            words = wordDao.queryPatWordByAll();
        }
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 初始化病人表格数据
        String[] columnNames = {"病人姓名","年纪","性别","病症","病房","负责老师"};
        String[][] data = new String[patientList.size()][6]; // 6列数据

        // 遍历患者列表，将每个患者的数据填入二维数组
        for (int i = 0; i < patientList.size(); i++) {
            Patient patient = patientList.get(i);
            data[i][0] = patient.getName();
            data[i][1] = String.valueOf(patient.getAge());
            data[i][2] = patient.getGender();
            data[i][3] = patient.getDiagnosis();
            data[i][4] = patient.getBedNumber();
            data[i][5] = patient.getDocId();
        }

        // 初始化治疗内容表格数据
        if(role.equals("病人")) {
            String[] workNames = {"治疗内容", "主治医生", "地点", "通知时间", "就诊时间"};
            String[][] workData = new String[words.size()][5];

            for (int i = 0; i < words.size(); i++) {
                PatWord word = words.get(i);
                workData[i][0] = word.getTitle();         // 治疗内容
                workData[i][1] = word.getDocId();         // 主治医生（假设 docId 存储医生名称）
                workData[i][2] = word.getPlace();      // 地点
                workData[i][3] = String.valueOf(word.getReleaseTime());   // 通知时间
                workData[i][4] = String.valueOf(word.getExecutionTime()); // 就诊时间
            }


            // 创建病人表格
            DefaultTableModel patientModel = new DefaultTableModel(workData, workNames);
            JTable patientTable = new JTable(patientModel);

            patientTable.setFont(new Font("宋体", Font.PLAIN, 14));
            patientTable.setRowHeight(30);
            patientTable.setShowHorizontalLines(false);
            patientTable.setShowVerticalLines(false);
            patientTable.setGridColor(new Color(230, 230, 230));


            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(JLabel.CENTER);
            for (int i = 0; i < patientTable.getColumnCount(); i++) {
                patientTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
            }

            DefaultTableModel workModel = new DefaultTableModel(data, columnNames);
            JTable workTable = new JTable(workModel);

            workTable.setFont(new Font("宋体", Font.PLAIN, 14));
            workTable.setRowHeight(30);
            workTable.setShowHorizontalLines(false);
            workTable.setShowVerticalLines(false);
            workTable.setGridColor(new Color(230, 230, 230));


            for (int i = 0; i < workTable.getColumnCount(); i++) {
                workTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
            }


            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);


            JScrollPane patientScrollPane = new JScrollPane(patientTable);
            patientScrollPane.setBorder(BorderFactory.createTitledBorder("治疗内容"));


            JScrollPane workScrollPane = new JScrollPane(workTable);
            workScrollPane.setBorder(BorderFactory.createTitledBorder("病人信息"));


            splitPane.setTopComponent(patientScrollPane);
            splitPane.setBottomComponent(workScrollPane);
            splitPane.setDividerLocation(300); // 设置初始分割位置
            splitPane.setResizeWeight(0.5);   // 设置调整大小时的权重


            panel.add(splitPane, BorderLayout.CENTER);
        }else{
            String[] workNames = {"病人姓名","治疗内容", "主治医生", "地点", "通知时间", "就诊时间"};
            String[][] workData = new String[words.size()][6];

            for (int i = 0; i < words.size(); i++) {
                PatWord word = words.get(i);
                workData[i][0] = word.getPatId();
                workData[i][1] = word.getTitle();         // 治疗内容
                workData[i][2] = word.getDocId();         // 主治医生（假设 docId 存储医生名称）
                workData[i][3] = word.getPlace();      // 地点
                workData[i][4] = String.valueOf(word.getReleaseTime());   // 通知时间
                workData[i][5] = String.valueOf(word.getExecutionTime()); // 就诊时间
            }


            // 创建病人表格
            DefaultTableModel patientModel = new DefaultTableModel(workData, workNames);
            JTable patientTable = new JTable(patientModel);

            patientTable.setFont(new Font("宋体", Font.PLAIN, 14));
            patientTable.setRowHeight(30);
            patientTable.setShowHorizontalLines(false);
            patientTable.setShowVerticalLines(false);
            patientTable.setGridColor(new Color(30, 230, 230));


            DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
            centerRenderer.setHorizontalAlignment(JLabel.CENTER);
            for (int i = 0; i < patientTable.getColumnCount(); i++) {
                patientTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
            }

            DefaultTableModel workModel = new DefaultTableModel(data, columnNames);
            JTable workTable = new JTable(workModel);

            workTable.setFont(new Font("宋体", Font.PLAIN, 14));
            workTable.setRowHeight(30);
            workTable.setShowHorizontalLines(false);
            workTable.setShowVerticalLines(false);
            workTable.setGridColor(new Color(230, 230, 230));


            for (int i = 0; i < workTable.getColumnCount(); i++) {
                workTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
            }


            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);


            JScrollPane patientScrollPane = new JScrollPane(patientTable);
            patientScrollPane.setBorder(BorderFactory.createTitledBorder("治疗内容"));


            JScrollPane workScrollPane = new JScrollPane(workTable);
            workScrollPane.setBorder(BorderFactory.createTitledBorder("病人信息"));


            splitPane.setTopComponent(patientScrollPane);
            splitPane.setBottomComponent(workScrollPane);
            splitPane.setDividerLocation(300); // 设置初始分割位置
            splitPane.setResizeWeight(0.5);   // 设置调整大小时的权重


            panel.add(splitPane, BorderLayout.CENTER);
        }
        return panel;
    }

    private JPanel createBottomPanel(JFrame mainFrame, String role,String username) {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(240, 240, 240));
        panel.setPreferredSize(new Dimension(800, 60));

        String labelText = "退出登录";
        if(role.equals("管理员")){
            labelText = "返回主页面";
        }
        JButton logoutBtn = new JButton(labelText);
        logoutBtn.setFont(new Font("宋体", Font.PLAIN, 14));
        logoutBtn.addActionListener(e -> {
            // 直接执行退出逻辑（无确认提示）
            mainFrame.dispose(); // 关闭当前主窗口
            if (role.equals("管理员")) {
                SwingUtilities.invokeLater(() -> {
                    AdminLoginExampleWindow adminLoginExample = new AdminLoginExampleWindow();
                    adminLoginExample.openMainPage(username, "管理员"); // 调用管理员登录窗口
                });
            } else {
                new LoginWindow(); // 打开普通登录窗口
            }
        });
        panel.add(logoutBtn);

        return panel;
    }

}